import java.io.UnsupportedEncodingException;
import java.rmi.RemoteException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.xml.rpc.ServiceException;
import javax.xml.rpc.holders.IntHolder;
import javax.xml.rpc.holders.LongHolder;
import javax.xml.rpc.holders.StringHolder;

import org.apache.axis.encoding.Base64;

import AllegroWebApi.AllegroWebApiPortType;
import AllegroWebApi.AllegroWebApiService;
import AllegroWebApi.AllegroWebApiServiceLocator;
import AllegroWebApi.holders.ArrayOfCatInfoStructHolder;
import AllegroWebApi.holders.ArrayOfSearchResponseHolder;

import com.mysql.jdbc.exceptions.MySQLNonTransientConnectionException;


public class ProductApi {
AllegroDAO dao = new AllegroDAO();
	
	public static void main(String[] args) {
	

		 // Dane u�ytkownika
		 String userLogin = "michbudz";
		 String userKey = "9cf13b3c";
		 int sysVar = 1; // webApi
		 int countryId = 1; // Polska
		 try {
		 String passDecoded = Base64.encode(MessageDigest.getInstance("SHA-256").digest("kpt9euxm".getBytes("UTF-8")));
		  
		 AllegroWebApiService webapi = new AllegroWebApiServiceLocator();
		 AllegroWebApiPortType port = webapi.getAllegroWebApiPort();
		
		 // Pobranie klucza wersji
		 LongHolder verKey = new LongHolder();
		 StringHolder info = new StringHolder();
		 port.doQuerySysStatus(sysVar, countryId, userKey, info, verKey);
		  
		 // Logowanie
		 StringHolder sessionHandlePart = new StringHolder();
		 LongHolder userId = new LongHolder();
		 LongHolder serverTime = new LongHolder();
		 port.doLoginEnc(
		 userLogin,
		 passDecoded,
		 countryId,
		 userKey,
		 verKey.value,
		 sessionHandlePart,
		 userId,
		 serverTime
		 );
		  
		 /*Metoda doShowCat
		 Pokazuje oferty w danej kategorii
		 */
		 ArrayOfSearchResponseHolder response = new ArrayOfSearchResponseHolder();
		 ArrayOfCatInfoStructHolder catParentArray = new ArrayOfCatInfoStructHolder();
		 ArrayOfCatInfoStructHolder catChildArray = new ArrayOfCatInfoStructHolder();
		 ArrayOfCatInfoStructHolder catSisterArray = new ArrayOfCatInfoStructHolder();
		 IntHolder catItemsCount = new IntHolder();
		 IntHolder catItemsCountFeatured = new IntHolder();
		 AllegroDAO dao = new AllegroDAO();
		ArrayList<Integer> catIdList = 	dao.getCategoryIds();
		 
		 for(Integer catId : catIdList){
			 try{
				 System.out.println("cat id : " + catId);
				  port.doShowCat(sessionHandlePart.value, catId ,0,1 /* tylko kup teraz*/,null,null,0,null,0,100,0,catParentArray,catChildArray,catSisterArray,catItemsCount,catItemsCountFeatured,response);
				 
				  dao.con.setAutoCommit(false);
				  dao.addOffers(response, catId);
				  
				  dao.con.commit();
			 }catch(MySQLNonTransientConnectionException e){
				 System.out.print(e.getMessage() + " cat id :" + catId);
			 }
			 catch(Exception e){
				 try {
					dao.con.rollback();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				 e.printStackTrace();
			 }finally{
				 try {
					dao.con.setAutoCommit(true);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			 }
		 
		 }
		 

		 } catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 

		 
		 
		 }
}
